package com.juc.phaser.phaser3;

import java.util.concurrent.Phaser;

public class PrintTools {

	/**
	 * arriveAndDeregister() 是使当前线程(运动员)退出比赛 
	 * 并且使parties值减一
	 */
    public static Phaser phaser;
	
	public static void methodA() {
		System.out.println(Thread.currentThread().getName() + " A1 begin= " + System.currentTimeMillis());
		phaser.arriveAndAwaitAdvance();
		System.out.println(Thread.currentThread().getName() + " A1 end= " + System.currentTimeMillis());
		System.out.println(Thread.currentThread().getName() + " A2 begin= " + System.currentTimeMillis());
		phaser.arriveAndAwaitAdvance();
		System.out.println(Thread.currentThread().getName() + " A2 end=  " + System.currentTimeMillis());
	}
	
	public static void methodB() {
		
		try {
			System.out.println(Thread.currentThread().getName() + " A1 begin= "+ System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("A : " + phaser.getRegisteredParties());
			phaser.arriveAndDeregister();
			System.out.println("B : " + phaser.getRegisteredParties());
			//phaser.arriveAndAwaitAdvance();
			System.out.println(Thread.currentThread().getName() + " A1 end= "+ System.currentTimeMillis());

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}
}
